package com.iyunwen.datatool.process;

import com.iyunwen.datatool.common.enums.DataSourceTypeEnum;
import com.iyunwen.datatool.dataanalysis.DataAnalysis;
import com.iyunwen.datatool.dataanalysis.DataAnalysisContext;
import com.iyunwen.datatool.dataexport.DataExport;
import com.iyunwen.datatool.dataexport.DataExportContext;
import com.iyunwen.datatool.dataload.LogLoader;
import com.iyunwen.datatool.dataload.LogLoaderContext;
import com.iyunwen.datatool.dataparse.DataParse;
import com.iyunwen.datatool.dataparse.DataParseContext;
import com.iyunwen.datatool.datastorage.DataStorageContext;
import com.iyunwen.datatool.model.vo.BaseNormalizationDataVO;
import com.iyunwen.datatool.model.vo.DataGroupVO;
import com.iyunwen.datatool.service.IDataLoadService;
import com.iyunwen.datatool.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description: 事件日志主线程
 * @author: Zhu.Guodong
 * @create: 2020-03-19 09:56
 * @version:
 */
@Slf4j
@Component
public class EventLogProcess {

    @Autowired
    LogLoaderContext logLoaderContext;

    @Autowired
    DataParseContext dataParseContext;

    @Autowired
    DataAnalysisContext dataAnalysisContext;

    @Autowired
    DataStorageContext dataStorageContext;

    @Autowired
    DataExportContext dataExportContext;

    @Autowired
    IDataLoadService dataLoadService;


    public void process() {


        String logFilePath = "/Users/zhuguodong/work/yunwen/devFiles/eventLog";
        String fileDirectionPath = "/Volumes/LongLongAgo/信号数据";
//        String fileDirectionPath = "D:/excel/event";
        //查找范围(毫秒)
        Long rangTime = 10000L;
        //读取文件类型
        DataSourceTypeEnum eventType = DataSourceTypeEnum.EVENT_LOG;

        String[] cellHead = {"标记", "事件时标", "类型", "主/从", "事件号", "事件来源", "事件描述", "事件等级", "状态", "持续时间", "设备描述", "操作者", "确认人"};
        List<String> cellHeadList = Arrays.asList(cellHead);
        //导出目录
        String exportPath = "/Volumes/LongLongAgo/eventGroup";
//        String exportPath = "D:/excel/export";

        long loadFileStartTime = System.currentTimeMillis();
        //需要解析的文件
        List<File> files = FileUtils.readFilesByPath(fileDirectionPath);
        //数据加载器
        LogLoader logLoader = logLoaderContext.getLogLoader(eventType.getIndex());
        //数据解析器
        DataParse dataParse = dataParseContext.getDataParse(eventType.getIndex());
        //数据结果
//        List<SignalEventVO> dataResult = new ArrayList<>();
//        SignalEventVO tableHead = null;
//        for (File file : files) {
//            List<SignalEventVO> signalEventVOList = logLoader.loadDataFromFile(file);
//            if (null == tableHead) {
//                tableHead = signalEventVOList.get(0);
//            }
//            signalEventVOList.remove(0);
//            dataResult.addAll(signalEventVOList);
//
//        }
        List<BaseNormalizationDataVO> dataFormatResult = new ArrayList<>();
        for (File file : files) {
            long fileLoadStartTime = System.currentTimeMillis();
            List<BaseNormalizationDataVO> baseNormalizationDataVOList = logLoader.loadDataFromFile(file, dataParse);
            log.info("文件名:[{}],数据量:{},解析耗时：{}ms", file.getName(), baseNormalizationDataVOList.size(), System.currentTimeMillis() - fileLoadStartTime);

            dataFormatResult.addAll(baseNormalizationDataVOList);

//            long itemStorageStartTime = System.currentTimeMillis();
//            log.info("单个文件入库开始入库");
//            dataStorage.insertAllDataToDatabase(dataFormatResult, tableName);
//            log.info("单个文件入库耗时:{}", System.currentTimeMillis() - itemStorageStartTime);
        }
        log.info("解析耗时:{}", System.currentTimeMillis() - loadFileStartTime);
        log.info("数据总量:{}", dataFormatResult.size());

        log.info("数据排序.....");
        long sortStartTime = System.currentTimeMillis();
        dataFormatResult = dataParse.sortData(dataFormatResult);
        log.info("排序完成,耗时:{}", System.currentTimeMillis() - sortStartTime);


//        long storageStartTime = System.currentTimeMillis();
//        DataStorage dataStorage = dataStorageContext.getDataStorage(eventType.getIndex());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        Date date = new Date(System.currentTimeMillis());
//        String tableName = "table" + sdf.format(date);
//        log.info("数据入库,表名:{}.....",tableName);
//        dataStorage.createTable(tableName);
//        dataStorage.insertAllDataToDatabase(dataFormatResult, tableName);
//        log.info("数据入库完成,耗时:{}", System.currentTimeMillis() - storageStartTime);

//        PageResult pageResult = dataLoadService.getEventData(1, 10, tableName);
//        EventDataDO eventDataDO = (EventDataDO) pageResult.getRows().get(0);
//        List<EventDataDO> list = dataLoadService.readSignalDataFromDb(eventDataDO, 10, tableName);

//        EventLogFieldEnum[] eventLogFieldEnums = new EventLogFieldEnum[];
//        eventLogFieldEnums[1] = EventLogFieldEnum.EVENT_REMARK;
//        eventLogFieldEnums[2] = EventLogFieldEnum.EVENT_TIME;

//        log.info("数据格式化.....");
//        long formatStartTime = System.currentTimeMillis();
//        List<BaseNormalizationDataVO> baseNormalizationDataVOS = dataParse.formatDataToNormalization(dataResult);
//        log.info("格式化完成，耗时:{}", System.currentTimeMillis() - formatStartTime);


        DataAnalysis dataAnalysis = dataAnalysisContext.getDataAnalysis(eventType.getIndex());

        DataExport dataExport = dataExportContext.getDataExport(eventType.getIndex());
        //设置导出参数
        dataExport.initParams(exportPath, cellHeadList);
        log.info("数据分组......");
        long groupStartTime = System.currentTimeMillis();
        List<DataGroupVO> groupData = dataAnalysis.analyzeDataToGroup(dataFormatResult);
        log.info("分组完成,分组数量:{}，耗时:{}", groupData.size(), System.currentTimeMillis() - groupStartTime);

        //数据导出
        log.info("数据导出......");
        long exportStartTime = System.currentTimeMillis();
//        DataExport dataExport = dataExportContext.getDataExport(eventType.getIndex());
        for (DataGroupVO itemGroupData : groupData) {
            //dataExport.dataExportToFile(itemGroupData, exportPath, cellHeadList);
            dataExport.dataExportToFile(itemGroupData);
        }
        log.info("分组导出，耗时:{}", System.currentTimeMillis() - exportStartTime);

        log.info("整体耗时:{}", System.currentTimeMillis() - loadFileStartTime);
    }

}
